/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sip;
/**
 *
 * @author Akanksha Chauhan
 */

//This is class used on the CheckThread. 
public class CheckStatus {
    Alarm alarm = new Alarm();
    InitSip initSip = new InitSip();
    public static Boolean bubbles = false;
    private static boolean startupFlag = false;
    private static boolean batteryLowFlag = false;
    private static boolean batteryFatalFlag = false;
    private static boolean insulinLowFlag = false;
    private static boolean insulinFatalFlag = false;
    private static boolean bubblesFlag = false;
    boolean[] insulinFlagArray = new boolean[2];
    boolean[] batteryFlagArray = new boolean[2];
    
    public synchronized boolean checkInsulinStatus(double insulinAmount)
    {
       if(InitSip.totalInsulinAmount < insulinAmount ) 
       {
           SipLogger.Instance().debug("The check was false");
           //SipLogger.Instance().debug(Double.toString(InitSip.totalInsulinAmount));
           //SipLogger.Instance().debug(Double.toString(insulinAmount ));
           return false; 
       }
       else{
           return true;
       }
       
    }
    public synchronized boolean startupCheck(){
        if (InitSip.BatteryLevel > 2){
            startupFlag =  true;
            
        }
        else
        {
            if(InitSip.BatteryLevel <= 2){
              //alarm.setAlarm("You have less than 3% of Battery left. Replace Batteries!");
              startupFlag =  false;
            }
            
        }
      return startupFlag;  
    }
    public synchronized boolean[] checkBattery(){
            
            //System.out.println("The thread is working");
            if(InitSip.BatteryLevel >= 3 && InitSip.BatteryLevel <= 15 ){
                //alarm.setAlarm("You Battery is Low!.");
                batteryLowFlag = true;
                batteryFatalFlag = false;
                
         
            }
           
            else if(InitSip.BatteryLevel <= 2){
                alarm.play();
                alarm.setAlarm("System preparing for Shutdown. Saving properties now...");
                initSip.writeCurrentValues();
                SipLogger.Instance().fatal("Battery Level is less 2 percent. Shutting down.");
                //alarm.play();
                SipLogger.Instance().debug("I played the alarm");
                batteryLowFlag = true;
                batteryFatalFlag = true;
                
                
               
            }
            else{
                batteryLowFlag = false;
                batteryFatalFlag = false;   
            }
            batteryFlagArray[0] = batteryFatalFlag;
            batteryFlagArray[1] = batteryLowFlag;
            //System.out.println(batteryFlagArray);
            return batteryFlagArray;
            
         
    }
    
    public synchronized boolean[] checkInsulin(){
        if(InitSip.totalInsulinAmount >= 2 && InitSip.totalInsulinAmount <= 15){
            
            insulinLowFlag = true;
            insulinFatalFlag = false;
        }

        else if(InitSip.totalInsulinAmount > 0 && InitSip.totalInsulinAmount <= 1){
            
            alarm.play();
            alarm.setAlarm("Insulin Reservoir is in fatal state. Preparing system for Shutdown!");
            insulinLowFlag = true;
            insulinFatalFlag = true;
            
        }
  
        else{
            insulinLowFlag = false;
            insulinFatalFlag = false;
        }
        
        insulinFlagArray[0] = insulinFatalFlag;
        insulinFlagArray[1] = insulinLowFlag;
        initSip.writeCurrentValues();
        return insulinFlagArray;
   
    }
    
    public synchronized boolean checkBubbles(){
        
        if(bubbles){
            //STOP Basal Inuslin Basal Thread here
            SipLogger.Instance().fatal("There are air bubbles on the Insulin Pump.");
            alarm.play();
            alarm.setAlarm("There are air bubbles on the Pump. Shutting down Insulin");
            bubbles = true;
            bubblesFlag = true;
            
              
        }
        else{
            bubbles = false;
            bubblesFlag = false;
            
        
        }
        initSip.writeCurrentValues();
        return bubblesFlag;
    
    }
    
    public int checkActiveProfile(){
        String lastActiveProfile;
        lastActiveProfile =  InitSip.mActiveProfileName;
        int res = 1;
        if(!lastActiveProfile.equals("")){
            res = alarm.confirm("Do you want to Activate: " + lastActiveProfile + " Basal Profile now?");
            
        }
        initSip.writeCurrentValues();
        return res;
    }
    
}


